# -*- coding: utf-8 -*-

import logging
from PyQt4.QtCore import (QThread, QMutex, QMutexLocker, SIGNAL)

class BookSeeker(QThread):
    def __init__(self, model, lock, index, value,  moreBooks, parent=None):
        super(BookSeeker, self).__init__(parent)
        self.model = model
        self.lock = lock
        self.index = index
        self.value = value
        self.stopped = False
        self.mutex = QMutex()
        self.completed = False
        self.moreBooks = moreBooks

    def stop(self):
        with QMutexLocker(self.mutex):
            self.stopped = True

    def isStopped(self):
        with QMutexLocker(self.mutex):
            return self.stopped

    def run(self):
        if (self.index == "Titulo"):
            logging.info(".....")
            self.model.search_by_title(self.value, self.moreBooks)
        elif (self.index == "Autor"):
            self.model.search_by_author(self.value, self.moreBooks)
        elif (self.index == "Editor"):
            self.model.search_by_publisher(self.value, self.moreBooks)
        elif (self.index == "ISBN"):
            self.model.search_by_isbn(self.value, self.moreBooks)
        elif (self.index == "Tema"):
            self.model.search_by_subject(self.value, self.moreBooks)

        self.emit(SIGNAL("finishDownloadBooks()"))

